<?php
/**
* @version $Id: jce.php,v 1.0 2006/03/20 13:53:08 happy_noodle_boy Exp $
* @package Joomla 1.0
* @Based on tinymce.php by stingrey
* @copyright (C) 2006 Ryan Demmer
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*/

/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$_MAMBOTS->registerFunction( 'onInitEditor', 'jceEditorInit' );
$_MAMBOTS->registerFunction( 'onGetEditorContents', 'jceEditorGetContents' );
$_MAMBOTS->registerFunction( 'onEditorArea', 'jceEditorArea' );

/**
* TinyMCE WYSIWYG Editor - javascript initialisation
*/
function jceEditorInit() {

        global $my, $database, $mainframe;

		require_once( $mainframe->getCfg('absolute_path') . "/mambots/editors/jce/jscripts/tiny_mce/libraries/classes/jce.class.php" );

		$jce = new JCE();
        $isSuperAdmin = $jce->isAdmin();
        $userid = $jce->id;
		$params = $jce->getParams();
		
		$editor_state 	= $params->get( 'editor_state', 'mceEditor' );
		$editor_toggle 	= $params->get( 'editor_toggle', '[show/hide]' );
    	
    	$toolbar 			= $params->get( 'toolbar', 'top' );
    	$html_height		= $params->get( 'html_height', '550' );
    	$html_width			= $params->get( 'html_width', '750' );
    	$text_direction		= $params->get( 'text_direction', 'ltr' );
    	$content_css		= $params->get( 'content_css', 1 );
    	$content_css_custom	= $params->get( 'content_css_custom', '' );
    	$invalid_elms	    = $params->get( 'invalid_elements', 'applet' );
    	$event_elms	        = $params->get( 'event_elements', '' );   	
		$newlines			= $params->get( 'newlines', 0 );
    	$preview_height		= $params->get( 'preview_height', '550' );
	    $preview_width		= $params->get( 'preview_width', '750' );
	    $editor_width       = $params->get( 'editor_width', '500' );
        $editor_height      = $params->get( 'editor_height', '600' );
        $table_inline       = $params->get( 'table_inline', 0 );
        $font_size_type     = $params->get( 'font_size_type', 'length' );
		$template_colors	= $params->get( 'template_colors', '' );
		$relative 			= ( $params->get( 'relative_url', '1' ) ) ? 'true' : 'false';
		$verify_html		= ( $params->get( 'verify_html', '1' ) ) ? 'true' : 'false';
        
        $table_inline = ( $table_inline ) ? 'true' : 'false';	
		
		$paste_params = $jce->getPluginParams( 'paste' );			
		$paste_create_paragraphs 		= $paste_params->get( 'paste_create_paragraphs', 'false' );
		$paste_create_linebreaks 		= $paste_params->get( 'paste_create_linebreaks', 'false' );
		$paste_use_dialog 				= $paste_params->get( 'paste_use_dialog', 'false' );
		$paste_auto_cleanup_on_paste 	= $paste_params->get( 'paste_auto_cleanup_on_paste', 'false' );
		$paste_strip_class_attributes 	= $paste_params->get( 'paste_strip_class_attributes', 'all' );
		$paste_remove_spans 			= $paste_params->get( 'paste_remove_spans', 'true' );
		$paste_remove_styles 			= $paste_params->get( 'paste_remove_styles', 'true' );

        // Get the default stylesheet
        $query = "SELECT template FROM #__templates_menu WHERE client_id='0' AND menuid='0'";
        $database->setQuery( $query );
        $cur_template = $database->loadResult();
        // Assigned template
        if ( isset( $Itemid ) && $Itemid != "" && $Itemid != 0 ) {
            $query = "SELECT template FROM #__templates_menu WHERE client_id='0' AND menuid='$Itemid' LIMIT 1";
            $database->setQuery( $query );
            $cur_template = $database->loadResult() ? $database->loadResult() : $cur_template;
        }
        //Plugin settings and Authorisation
        $template_path = $mainframe->getCfg('live_site') . "/templates/" . $cur_template . "/css";
        $css_template = ( $content_css ) ? $template_path . "/template_css.css" : $template_path . "/" . $content_css_custom;
		
		$invalid_elements[] = $invalid_elms; 
        $elements = $jce->getElements(); 
		
		if( !$jce->getAuthOption( 'allow_script', '0' ) ){
			$invalid_elements[] = 'script';
		} else{
			$elements = $jce->addKey( $elements, 'script[*]', ',' );
			$jce->removeKey( $invalid_elements, 'script' );
		}
		//Mutually exclusive plugins
		$me_plugins = array('imgmanager', 'advlink', 'mediamanager');

		if( $jce->isLoaded( 'mediamanager' ) || $jce->isLoaded( 'mediamanager_se' ) ){
			if( $jce->isLoaded( 'mediamanager' ) ){
				$mm_params = $jce->getPluginParams( 'mediamanager' );
			}
			if( $jce->isLoaded( 'mediamanager_se' ) ){	
				$mm_params = $jce->getPluginParams( 'mediamanager_se' );
			}	
				$mm_use_script = ( $mm_params->get( 'use_script', 'false' ) ) ? 'true' : 'false';
				if( $mm_use_script && !$jce->getAuthOption( 'allow_script', '0' ) ){
					$elements = $jce->addKey( $elements, 'script[*]', ',' );
					$jce->removeKey( $invalid_elements, 'script' );
				}
		}else{
			$mm_use_script = 'false';
		}

        $plugins = $jce->getPlugins( $me_plugins );
		$remove_buttons = $jce->getRemovePlugins();
        
        $row1 = $jce->getRow( 1 );
		$row2 = $jce->getRow( 2 );
		$row3 = $jce->getRow( 3 );
		$row4 = $jce->getRow( 4 );
		$row5 = $jce->getRow( 5 );
    
        $jce_langs = $jce->getLangs();
		$jce_curr_lang = $jce->getLanguage();
		$invalid_elements = implode(',', $invalid_elements );

	    if ( $newlines ) {
		    $br_newlines	= 'true';
		    $p_newlines     = 'false';
    	} else {
    		$br_newlines	= 'false';
    		$p_newlines     = 'true';
    	}
    	$font_size_type = ( $font_size_type == 'length' ) ? '8pt,10pt,12pt,14pt,18pt,24pt,36pt' : 'xx-small,x-small,small,medium,large,x-large,xx-large';
        
		$base_url = $mainframe->getCfg('live_site');
		$tiny_url = $jce->getTinyUrl();
		
		$tiny_file = ( $params->get('compression', '0') ) ? 'tiny_mce_gzip.js' : 'tiny_mce.js';
		
		if( $params->get('compression', '0') ){
			$tiny_file = 'tiny_mce_gzip.js';
			$gzip_init = "<script type=\"text/javascript\">\n";
			$gzip_init .= "	tinyMCE_GZ.init({\n";
			$gzip_init .= "	plugins : '" . $plugins . "',\n";
			$gzip_init .= "	themes : 'advanced',\n";
			$gzip_init .= "	languages : '" . $jce_curr_lang . "',\n";
			$gzip_init .= "	disk_cache : false,\n";
			$gzip_init .= "	debug : false\n";
			$gzip_init .= "});\n";
			$gzip_init .= "</script>\n"; 
		}else{
			$tiny_file = 'tiny_mce.js';
			$gzip_init = '';
		}		
		return <<<EOD
<!--//TinyMCE/JCE-->
<script type="text/javascript" src="$tiny_url/$tiny_file"></script>
<script type="text/javascript" src="$tiny_url/jce.functions.js"></script>
$gzip_init
<script type="text/javascript">
	var site_url = jceFunctions.getURL();
	jceFunctions.relative = $relative;
	jceFunctions.state = "$editor_state";
	jceFunctions.toggle = "$editor_toggle";
	tinyMCE.init({
		site : site_url,
		document_base_url: "$base_url/",
		theme : "advanced",
		language : "$jce_curr_lang",
		lang_list : "$jce_langs",
		width : "$editor_width",
		height : "$editor_height",
		mode : "specific_textareas",
		browsers : "msie,safari,gecko,opera",
		event_elements : "$event_elms",
		relative_urls : false,
		remove_script_host : false,
		entity_encoding : "raw",
		verify_html : $verify_html,
		remove_linebreaks : false,
		apply_source_formatting : true,
		fix_list_elements : true,
		fix_table_elements : true,
		convert_fonts_to_spans : true,
		inline_styles : true,
		save_callback : "jceFunctions.save",
		content_css : "$css_template",
		//Paste Options
		paste_create_paragraphs : $paste_create_paragraphs,
		paste_create_linebreaks : $paste_create_linebreaks,
		paste_use_dialog : $paste_use_dialog,
		paste_auto_cleanup_on_paste : $paste_auto_cleanup_on_paste,
		paste_strip_class_attributes : "$paste_strip_class_attributes",
		paste_remove_spans : $paste_remove_spans,
		paste_remove_styles : $paste_remove_styles,
		//
		template_colors : "$template_colors",
		media_use_script : $mm_use_script,
		font_size_style_values : "$font_size_type",
		table_inline_editing : $table_inline,
		invalid_elements : "$invalid_elements",
		force_br_newlines : $br_newlines,
		force_p_newlines : $p_newlines,
		directionality : "$text_direction",
		theme_advanced_layout_manager : "SimpleLayout",
		theme_advanced_resizing : true, 
		theme_advanced_toolbar_location : "top",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_disable : "$remove_buttons",
		theme_advanced_buttons1 : "$row1",
		theme_advanced_buttons2 : "$row2",
		theme_advanced_buttons3 : "$row3",
		theme_advanced_buttons4 : "$row4",
		theme_advanced_buttons5 : "$row5",
		plugins : "$plugins",
		extended_valid_elements : "$elements"
	});
</script>
EOD;
}
/**
* TinyMCE WYSIWYG Editor - copy editor contents to form field
* @param string The name of the editor area
* @param string The name of the form field
*/
function jceEditorGetContents( $editorArea, $hiddenField ) {
        global $jspath;

        return <<<EOD
        tinyMCE.triggerSave();
EOD;
}
/**
* mosce WYSIWYG Editor - display the editor
* @param string The name of the editor area
* @param string The content of the field
* @param string The name of the form field
* @param string The width of the editor area
* @param string The height of the editor area
* @param int The number of columns for the editor area
* @param int The number of rows for the editor area
*/
function jceEditorArea( $name, $content, $hiddenField, $width, $height, $col, $row ) {
        global $_MAMBOTS, $mainframe;
        
        $results = $_MAMBOTS->trigger( 'onCustomEditorButton' );
        $buttons = array();
        foreach ($results as $result) {
               $buttons[] = '<img src="'.$mainframe->getCfg('live_site').'/mambots/editors-xtd/'.$result[0].'" onclick="tinyMCE.execCommand(\'mceInsertContent\',false,\''.$result[1].'\')" />';
        }
            $buttons = implode( "", $buttons );
            
        return <<<EOD
<a id="editor_toggle" href="javascript:jceFunctions.toggleEditor('$hiddenField');">[show/hide]</a>
<textarea id="$hiddenField" name="$hiddenField" cols="$col" rows="$row" style="width:{$width}px; height:{$height}px;" mce_editable="true" class="mceEditor">$content</textarea>
<script type="text/javascript">
		jceFunctions.initEditorMode('$hiddenField');
</script>
<br />$buttons
EOD;
}
?>
